#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def get_ids(filename):
    import json

    f = file(filename)
    s = json.load(f)
    f.close()
    ids = ""
    for id in s["esearchresult"]["idlist"]:
        ids += id + ","
    ids = ids[:-1]
    return ids


def getPMC(id, ids):
    import pycurl
    import StringIO

    url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
    buf = StringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(pycurl.POST, 1)
    c.setopt(c.URL, url)
    c.setopt(pycurl.HTTPPOST, [('db', 'nuccore'), ('id', str(ids)), ('rettype', 'fasta')])
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.perform()
    outfile = open("hmp_" + id + ".fasta", "w")
    outfile.write(buf.getvalue())
    outfile.flush()
    outfile.close()
    buf.close()
    print "HMP task id " + id + " has been successfully downloaded."


if __name__ == '__main__':
    for i in xrange(1, 22):
        filename = "hmp_" + str(i) + ".json"
        id = str[i]
        ids = get_ids(filename)
        getPMC(id, ids)